import i18n from '@/i18n'
import Vue from 'vue'

import service from '@/service/service'

let vueI18n = new Vue({
  i18n
})

const satellite = {
  state: {
    checkRules: {
      name: [
        // 请输入卫星名称
        { required: true, message: vueI18n.$t('message.validate.resources.satellite.name'), trigger: 'blur' }
      ],
      distance: [
        // 请输入距离
        { required: true, message: vueI18n.$t('message.validate.resources.satellite.distance'), trigger: 'blur' },
        // 请输入数字
        { pattern: service.regExp.float, message: vueI18n.$t('message.validate.common.number'), trigger: 'blur' }
      ],
      longitude: [
        // 请输入经度
        { required: true, message: vueI18n.$t('message.validate.resources.satellite.longitude'), trigger: 'blur' }
      ],
      latitude: [
        // 请输入纬度
        { required: true, message: vueI18n.$t('message.validate.resources.satellite.latitude'), trigger: 'blur' }
      ],
      gt: [
        // 请输入gt
        { required: true, message: vueI18n.$t('message.validate.resources.satellite.gt'), trigger: 'blur' },
        // 请输入数字
        { pattern: service.regExp.float, message: vueI18n.$t('message.validate.common.number'), trigger: 'blur' }
      ]
    },

    satelliteList: [],

    effectiveSatelliteList: [],
    filteredEffectiveSatelliteList: [],
    cachedSatellite: {
      name: ''
    }
  },
  mutations: {
    setSatelliteList (state, data) {
      state.satelliteList = data
    },

    setEffectiveSatelliteList (state, data) {
      state.effectiveSatelliteList = data
    },
    setFilteredEffectiveSatelliteList (state, data) {
      let filterIds = JSON.parse(localStorage.getItem('filterSatelliteIds')) || []
      state.filteredEffectiveSatelliteList = data.filter(satelliteItem => {
        return !filterIds.find(idItem => {
          return satelliteItem.id === idItem
        })
      })
    },
    setCachedSatellite (state, data) {
      let cachedSatelliteId = JSON.parse(localStorage.getItem('cachedSatelliteId')) || ''
      let cachedSatellite = data.find(satelliteItem => {
        return satelliteItem.id === cachedSatelliteId
      })
      // 当effective列表中找不到缓存的ID对应的卫星时，清空缓存
      if (!cachedSatellite) {
        localStorage.removeItem('cachedSatelliteId')
      }
      if (cachedSatellite) {
        state.cachedSatellite = cachedSatellite
      } else {
        state.cachedSatellite = data[0]
      }
    }
  },
  actions: {
    async getSatelliteList (context) {
      let response = await context.dispatch(`axios`, {
        method: 'get',
        url: '/api/abs/resources/satellites/'
      })
      context.commit('setSatelliteList', response.data)
      return 1
    },
    async getEffectiveSatelliteList (context) {
      let response = await context.dispatch(`axios`, {
        method: 'get',
        url: '/api/abs/resources/effective_satellite/'
      })
      context.commit('setEffectiveSatelliteList', response.data)
      context.commit('setFilteredEffectiveSatelliteList', response.data)
      context.commit('setCachedSatellite', response.data)
      return 1
    }
  }
}
export default satellite
